package com.matol.util;

import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Pattern;

public class Tools {
	public static void main(String[] args) {
		//System.out.print(getProperties("url"));
	}
	
	//时间格式刷
	public static SimpleDateFormat sday = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat stime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	//价格格式刷
	public static DecimalFormat df = new DecimalFormat("0.00");
	
	/*
	 * 根据名称获取配置文件的值
	 */
	public static String getProperties(String name){
		String str = "";
		
		Properties p;
		try {
			InputStream inputStream = Tools.class.getResourceAsStream("/config.properties");        
     
			p = new Properties();  
			p.load(inputStream);  
			inputStream.close();
			
			str = p.getProperty(name);
		} catch (Exception e) { Tools.Logs("##[getProperties]##"+e.getMessage()); }   
		
		return str;
	}
	
    //获取当前时间
    public static Date getDate(){
    	Date date = new Date();
    	//由于本地和服务器相差6小时，而程序中需要按照特定时间段进行判断，需要进行时间处理
		//date.setHours(date.getHours()+18);
		
		return date;
    }
    
    private static File file = null; 
    /*
     * 系统Log日志
     */
	public static void Logs(String str){		
		try {
			String path = getProperties("logs")+"//"+sday.format(getDate())+".txt";
			if(file == null){
				file = new File(path);
				if(!file.getParentFile().exists()) {  
	                file.getParentFile().mkdirs();  
	            } 
			}
			
            FileWriter fw = new FileWriter(path,true); 
            PrintWriter pw = new PrintWriter(fw);
            
    		if("".equals(str)){
    			System.out.println("");
    			pw.println("\r");
    		}else{
    			System.out.println("["+stime.format(getDate())+"]"+str);
    			pw.println("["+stime.format(getDate())+"]"+str+"\r");
    		}

			pw.flush();
			fw.flush();
			pw.close();
			fw.close();
		} catch (Exception e) {System.out.println("##[Logs]##"+e.getMessage());}
	}
	
	/*
	 * 判定是否为数字
	 */
	public static int isNumber(String str){ 
	    Pattern pattern = null;
	    int i = 0;
		try {
			pattern = Pattern.compile("[0-9]*");
			if(pattern.matcher(str).matches()){
				i = (int)Integer.parseInt(str);
			}
		} catch (Exception e) { Tools.Logs("##[isNumber]##"+e.getMessage()); } 
	    return i;    
	 } 

}
